package com.karry.mapper;

import com.karry.pojo.Blog;
import com.karry.pojo.SensitiveWord;
import com.karry.vo.BlogVo;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

public interface BlogMapper {

    /*
    *   查找全部博客
    * */
    @Select("select * from tb_blog;")
    @ResultMap("blogResultMap")
    ArrayList<Blog> getAllBlog();

    /**
     *  查询全部敏感词（临时放在这里）
     * */
    @Select("select * from demand_sensitive_word;")
    @ResultMap("sensitiveResultMap")
    ArrayList<SensitiveWord> searchAllWord();

    /**
     *  修改博客为私密
     * */
    @Update("update tb_blog set b_is_open = 0 where b_id = #{id};")
    int updateBlogToPrivate(int id);


    /**
     *
     * 查询全部公开博客，用于违规词检测
     * */
    @Select("select * from tb_blog where b_is_open = 1;")
    @ResultMap("blogResultMap")
    ArrayList<Blog> searchAllOpenBlog();


    /**
     * 根据用户id获取博客
     */
    @Select("select * from tb_blog where b_user_id = #{id} and b_status = 1;")
    @ResultMap("blogResultMap")
    List<Blog> searchBlogListByUserID(@Param("id") int id);

    /**
     *  根据id获取博客
     */
    @Select("select * from tb_blog where b_id = #{id} and b_status = 1;")
    @ResultMap("blogResultMap")
    Blog searchBlogById(@Param("id") Integer id);

    //无视状态获取博客通过ID
    @Select("select * from tb_blog where b_id = #{id};")
    @ResultMap("blogResultMap")
    Blog searchBlog(int id);


    /**
     *  查询满足相关要求的博客总数
     * @param id        用户id
     * @param title     标题（模糊匹配）
     * @param date      时间（年月）
     * @param typeId    类型
     * @param r         是否公开
     */
    long findBlogCount(@Param("id") Integer id, @Param("title") String title, @Param("date") String date, @Param("typeId") String typeId, @Param("open") int r);

    /**
     *  分页查询满足相关要求的博客
     * @param id        用户id
     * @param title     标题（模糊匹配）
     * @param date      时间（年月）
     * @param typeId    类型
     * @param r         是否公开
     * @param index     开始值
     * @param pageSize  单页数量
     */
    List<Blog> findBlogListByPage(@Param("id") Integer id, @Param("index") Integer index, @Param("pageSize") Integer pageSize, @Param("title") String title, @Param("date") String date, @Param("typeId") String typeId, @Param("open") int r);

    /**
     * 根据id查询博客，使用时间（年月）分类
     */
    @Select("select count(*) groupCount, DATE_FORMAT(b_time,'%Y年%m月') groupName from tb_blog where b_user_id = #{id} and b_status = 1 GROUP BY DATE_FORMAT(b_time,'%Y年%m月') ORDER BY DATE_FORMAT(b_time,'%Y年%m月') DESC;")
    @ResultMap("blogVoResultMap")
    List<BlogVo> findBlogCountByDate(@Param("id") int id);

    @Select("select count(*) groupCount, b_user_id groupName from tb_blog where b_status = 1 GROUP BY b_user_id ORDER BY b_user_id DESC;")
    @ResultMap("blogVoResultMap")
    List<BlogVo> findBlogCountByUser();

    /**
     * 根据id查询博客，使用类型分类
     */
    @Select("select count(b.b_id) groupCount, b.b_type_id as typeId, t.t_name as groupName from tb_blog b inner join tb_type t on b.b_type_id = t.t_id where b.b_user_id = #{id} and b_status = 1 GROUP BY b.b_type_id ORDER BY COUNT(b.b_id) DESC;")
    @ResultMap("blogVoResultMap")
    List<BlogVo> findBlogCountByType(@Param("id") int id);

    /**
    *  插入博客
    * */
   /* @Insert("insert into tb_blog (b_type_id, b_type_name, b_user_id, b_user_name, b_title, b_content, b_lon, b_lan, b_is_open) values (" +
            " #{tId}, #{tName}, #{uId}, #{uName}, #{title}, #{content}, #{lon}, #{lan}, #{isOpen});")
    int insertBlog(@Param("tId") int tId, @Param("tName") String name, @Param("title") String title, @Param("content") String content, @Param("uId") int id, @Param("uName") String name1, @Param("lon") float lonN, @Param("lan") float latN, @Param("isOpen") int isOpen);
*/
    //@Insert("insert into tb_blog (b_type_id, b_type_name, b_user_id, b_user_name, b_title, b_content, b_lon, b_lan, b_is_open) values (" +
            //" #{blog.typeID}, #{blog.typeName}, #{blog.userID}, #{blog.userName}, #{blog.title}, #{blog.content}, #{blog.lon}, #{blog.lan}, #{blog.isOpen});")
    //使用新方法，插入后返回主键值
    int insertBlog(@Param("blog") Blog blog);


    /**
     *  修改博客
     * */
    @Update("update tb_blog set b_type_id = #{tId}, b_type_name = #{tName}, b_title = #{title}, b_content = #{content}, b_user_id = #{uId}, b_user_name = #{uName}, b_lon = #{lon}, b_lan = #{lan}, b_last_update_time = default, b_is_open = #{isOpen} where b_id = #{bId};")
    int updateBlog(@Param("bId") Integer bId,@Param("tId") int tId, @Param("tName") String name, @Param("title") String title, @Param("content") String content, @Param("uId") int id, @Param("uName") String name1, @Param("lon") float lonN, @Param("lan") float latN, @Param("isOpen") int isOpen);

    /**
     *  删除博客
     * */
    @Update("update tb_blog set b_status = 0 where b_id = #{id};")
    int deleteBlog(int id);

    @Select("select count(*) from tb_blog;")
    int getBlogCount();

    @Select("select * from tb_blog limit #{i}, 10;")
    @ResultMap("blogResultMap")
    ArrayList<Blog> getBlogListByPage(int i);

    //博客设为私密
    @Update("update tb_blog set b_is_open = 0 where b_id = #{id};")
    int closeBlog(int id);

    //博客设为开放
    @Update("update tb_blog set b_is_open = 1 where b_id = #{id};")
    int openBlog(int id);


    //逻辑恢复博客
    @Update("update tb_blog set b_status = 1 where b_id = #{id};")
    int liveBlog(int id);


    //增加浏览量
    @Update("update tb_blog set b_view_count = b_view_count + 1 where b_id = #{id};")
    int addViewCount(int id);


}
